/**
 * Created with IntelliJ IDEA.
 * User: duoba
 * Date: 16/2/24
 * Time: 下午3:30
 * To change this template use File | Settings | File Templates.
 */

package com.cake21.uitest.System.Core;

import java.util.Iterator;

import org.openqa.selenium.WebDriver;
import com.cake21.uitest.System.Driver.PageDriver;
import com.cake21.uitest.System.Map.BrowserMap;
import com.cake21.uitest.System.Map.CaseMap;

public class Browser extends Thread {
    /*浏览器名称*/
    private String name = "";

    public String getNameX() {
        return name;
    }

    /*浏览器类型*/
    private int type = 1;

    public int getType() {
        return type;
    }

    /*浏览器版本*/
    private String version = "";

    public String getVersion() {
        return version;
    }

    /*浏览器平台*/
    private String platform = "";

    public String getPlatform() {
        return platform;
    }

    /*测试判断*/
    private Judge judge = new Judge();

    public Judge getJudge() {
        return judge;
    }

    /*浏览器Driver*/
    private WebDriver driver = null;

    public WebDriver getDriver() {
        return driver;
    }

    /*初始化浏览器*/
    public Browser initial(String key) {
        /*根据索引获取浏览器名、版本号、系统平台配置*/
        name = BrowserMap.getMap().get(key).get("name");
        type = Integer.parseInt(BrowserMap.getMap().get(key).get("type"));
        version = BrowserMap.getMap().get(key).get("version");
        platform = BrowserMap.getMap().get(key).get("platform");
        pageLoadTimeout = initPageLoadTimeout(key);
        scriptTimeout = initScriptTimeout(key);
        implicitlyWait = initImplicitlyWait(key);
        return this;
    }

    /*页面超时时间*/
    private Integer pageLoadTimeout = 30;

    public Integer getPageLoadTimeout() {
        return pageLoadTimeout;
    }

    /*初始化页面超时时间*/
    private Integer initPageLoadTimeout(String key) {
        Integer pageLoadTimeout = Server.getPageLoadTimeout();
        try {
            /*读取配置错误或无配置时，使用全局配置*/
            String pageLoadValue = BrowserMap.getMap().get(key).get("pageLoadTimeout");
			/*覆盖全局配置*/
            pageLoadTimeout = (!pageLoadValue.equals("")) ? Integer.parseInt(pageLoadValue) : Server.getPageLoadTimeout();
        } catch (Exception e) {
			/*配置设置错误*/
            e.printStackTrace();
        }
        return pageLoadTimeout;
    }

    /*等待超时时间*/
    private Integer implicitlyWait = 10;

    public Integer getImplicitlyWait() {
        return implicitlyWait;
    }

    /*初始化等待超时时间*/
    private Integer initImplicitlyWait(String key) {
        Integer implicitlyWait = Server.getImplicitlyWait();
        try {
			/*读取配置错误或无配置时，使用全局配置*/
            String implicitnessValue = BrowserMap.getMap().get(key).get("implicitlyWait");
			/*覆盖全局配置*/
            implicitlyWait = (!implicitnessValue.equals("")) ? Integer.parseInt(implicitnessValue) : Server.getImplicitlyWait();
        } catch (Exception e) {
			/*配置设置错误*/
            e.printStackTrace();
        }
        return implicitlyWait;
    }

    /*脚本超时时间*/
    private Integer scriptTimeout = 30;

    public Integer getScriptTimeout() {
        return scriptTimeout;
    }

    /*初始化脚本时间*/
    private Integer initScriptTimeout(String key) {
        Integer scriptTimeout = Server.getScriptTimeout();
        try {
			/*读取配置错误或无配置时，使用全局配置*/
            String scriptValue = BrowserMap.getMap().get(key).get("scriptTimeout");
			/*覆盖全局配置*/
            scriptTimeout = (!scriptValue.equals("")) ? Integer.parseInt(scriptValue) : Server.getScriptTimeout();
        } catch (Exception e) {
			/*配置设置错误*/
            e.printStackTrace();
        }
        return scriptTimeout;
    }

    /*浏览器顺序测试*/
    public void launch() {
        /*写入Report开始*/
        judge.info("浏览器名称:" + name + " 浏览器版本: " + version + " >>>> 启动");
        Server.getCommander().getThreadMap().put(Thread.currentThread().getId(), this);
		/*写入测试浏览器信息*/
        Server.getCommander().getJudgeMap().put(name + " [ Version:" + version + " Platform:" + platform + " ]" + " ----- " + Server.currentTime(), judge);
        /*循环取Case并运行*/
        Iterator<String> caseIterator = CaseMap.getMap().keySet().iterator();
        while (caseIterator.hasNext()) {
            /*开启浏览器*/
            driver = new PageDriver(type).getDriver();
            /*浏览器窗口最大化*/
            driver.manage().window().maximize();
            /*运行*/
            runCase(caseIterator.next());
            /*关闭浏览器*/
            driver.close();
            /*退出浏览器*/
            driver.quit();
        }
        /*写入Report结束*/
        judge.info("浏览器名称:" + name + "浏览器版本: " + version + " >>>> 关闭");
    }

    /*运行用例*/
    private void runCase(String caseKey) {
        try {
            String caseScript = CaseMap.getMap().get(caseKey).get("script");
			/*根据索引获取用例文件位置并实例化*/
            Case testCase = (Case) Class.forName("com.cake21.uitest.Case." + caseScript).newInstance();
            testCase.initial(caseKey).launch();
        } catch (Exception e) {
			/*运行Case出错*/
            e.printStackTrace();
        }
    }
}